package service

import (
	"database/sql"
	"log"
)

//数据库连接参数
const db_name = "mysql"
const db_url = "video:video@tcp(127.0.0.1:3306)/golang_test?parseTime=true"

//数据库连接struct
type DB struct {
	Conn *sql.DB //数据库连接对象
}

//初始化数据库连接
func InitDB() DB {

	var db DB
	var err error
	//初始化数据库连接
	db.Conn, err = sql.Open(db_name, db_url)
	if err != nil {
		log.Fatal(err)
	}
	log.Println("db open OK")
	//测试数据库连接
	err = db.Conn.Ping()
	if err != nil {
		log.Fatal(err)
	}
	log.Println("db connection OK")

	return db
}
